﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Common.Model;
using Microsoft.Windows.Controls;
using System.Data.Objects.DataClasses;

namespace Common.UI.EM.Agregation
{
    /// <summary>
    /// Interaction logic for AgregationControl.xaml
    /// </summary>
    public partial class AgregationListView : UserControl, IAgregationListView 
    {

        public AgregationDetailDataView CustomControl
        {
            get { return (AgregationDetailDataView)GetValue(CustomControlProperty); }
            set { SetValue(CustomControlProperty, value); }
        }

        public static readonly DependencyProperty CustomControlProperty =
            DependencyProperty.Register("CustomControl", typeof(AgregationDetailDataView), typeof(AgregationListView), new UIPropertyMetadata(null, new PropertyChangedCallback(CustomControl_Changed)));

        private static void CustomControl_Changed(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            AgregationListView agregationView = (AgregationListView)o;
            agregationView.regionContentControl.Content = e.NewValue;
        }

        public AgregationListView()
        {
            InitializeComponent();
        }

        public EntityObject GetSelectedEntity()
        {
            if (this.agregationDataGrid.SelectedItem == null)
            {
                MessageBox.Show("Debe seleccionar al menos un item de la grilla.", "Atención", MessageBoxButton.OK);
                return null;
            }

            return this.agregationDataGrid.SelectedItem as EntityObject;
        }

        public void AddColumnDefinition(String columnName, String header)
        {
            var column = new DataGridTextColumn();

            column.Header = header;
            column.Binding = new Binding(columnName);
            column.IsReadOnly = true;

            this.agregationDataGrid.Columns.Add(column);
        }

        public void UpdateListView()
        {
            this.agregationDataGrid.UpdateLayout();
        }
    }
}
